/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-20
*/
package com.rzico.core.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.core.entity.SysMenu;
import com.rzico.core.entity.SysUser;
import com.rzico.core.service.SysUserService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.core.entity.SysReportCategory;
import com.rzico.core.service.SysReportCategoryService;
import com.rzico.util.CodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;

import java.util.*;

/**
 * 统计报表控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-04-20
 */
@Api(description = "统计报表接口")
@RestController
@RequestMapping("/admin/sysReportCategory")
public class SysReportCategoryController extends BaseController {

    @Autowired
    private SysReportCategoryService sysReportCategoryService;

    @Autowired
    private SysUserService sysUserService;
    /**
     * 分页查询统计报表
     *
     * @return
    */
    @ApiOperation("分页查询统计报表")
    @GetMapping("/list")

    public CommResult<SysReportCategory> list(String startDate, String endDate, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sortField", "tree_path");
        params.put("sortType", "asc");
        SysUser sysUser = sysUserService.findByUsername("admin");
        if (sysUser==null) {
            return CommResult.error("没有登录");
        }
        params.put("mchId",sysUser.getMchId());
        try {
            List<SysMenu> list = sysReportCategoryService.selectTree(params);
            return CommResult.success(list);
        } catch (Exception e) {
            return CommResult.error("查询出错");
        }
    }

    /**
     * 查询单条统计报表
     *
     * @return
    */
    @ApiOperation("查询单条统计报表")
    @GetMapping("/find/{id}")
    public CommResult<SysReportCategory> find(@PathVariable String id) {
        SysReportCategory result = sysReportCategoryService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("sysReportCategory",result);
        return CommResult.success(data);

    }

    /**
     * 保存统计报表
     *
     * @param sysReportCategory
     * @return
     */
    @ApiOperation("保存统计报表")
    @PostMapping("/save")
    public CommResult<SysReportCategory> save(@RequestBody SysReportCategory sysReportCategory) {
        sysReportCategory.setId(CodeGenerator.getUUID());
        sysReportCategory.setCreateDate(new Date());
        sysReportCategory.setIsShow(true);
        SysUser sysUser = sysUserService.getCurrent();
        if (sysUser!=null) {
            sysReportCategory.setMchId(sysUser.getMchId());
        }
        int affectCount = sysReportCategoryService.insert(sysReportCategory);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        SysReportCategory result = sysReportCategoryService.findById(sysReportCategory.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除统计报表
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除统计报表", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除统计报表,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<SysReportCategory> del(@PathVariable String ids) {

        sysReportCategoryService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新统计报表
     *
     * @param sysReportCategory
     * @return
     */
    @Log(desc = "更新统计报表", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新统计报表")
    @PostMapping("/update")
    public CommResult<SysReportCategory> update(@RequestBody SysReportCategory sysReportCategory) {
        int affectCount = sysReportCategoryService.updateByPrimaryKeySelective(sysReportCategory);
        if (affectCount <= 0){
            return CommResult.error();
        }
        SysReportCategory result = sysReportCategoryService.findById(sysReportCategory.getId());
        return CommResult.success(result);
    }

}
